import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Question2 extends JFrame {
    public Question2() {
        setTitle("一起聊天吧");
        setSize(350, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel contentPane = new JPanel();    //创建一个JPanel对象
        setContentPane(contentPane);
        JTextArea jta = new JTextArea(7, 30);
        jta.setLineWrap(true);    //设置文本域中的文本为自动换行
        JScrollPane jsp = new JScrollPane(jta);    //将文本域放入滚动窗口
        Dimension size = jta.getPreferredSize();    //获得文本域的首选大小
        jsp.setBounds(110, 90, size.width, size.height);
        contentPane.add(jsp);    //将JScrollPane添加到JPanel容器中

        JLabel label1 = new JLabel();
        label1.setText("请输入：");
        JTextField field = new JTextField(20);
        JButton button = new JButton("提交");
        //内部类形式,按钮添加监听事件
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String fieldText = field.getText();//取值
                field.setText("");//置空
                jta.append(fieldText + "\r\n");//显示并换行
            }
        });
        contentPane.add(label1);
        contentPane.add(field);
        contentPane.add(button);

    }

    public static void main(String[] args) {
        Question2 frame = new Question2();
        frame.setVisible(true);
    }
}
